<template>
	<view class="coupon-index">
		<view class="coupon-list" v-for="(item,index) in datalist" :key="index" @click.stop="handleView(item)"
			:style="{background:'linear-gradient(270deg,'+t('color1')+' 0%,rgba('+t('color1rgb')+',0.8) 100%)'}">
			<view class="coupon-list-left">
				<image class="coupon-list-left-img" :src="item.active_pic" mode=""></image>
				<!-- <view class="money-overlay">
					￥{{item.coupon_sumMoney}}
				</view> -->
			</view>
			<view class="coupon-list-right">
				<view class="coupon-list-right-title">
					<view class="coupon-name">
						{{item.name}}
					</view>

					<view class="coupon-list-right-time">
						{{item.starttime.split(' ')[0]}}-{{item.endtime.split(' ')[0]}}
					</view>
					<view class="" v-if="item.type == 1">
						新人专享活动
					</view>
					<view class="" v-if="item.type == 2">
						社群营销活动
					</view>
					<view class="" v-if="item.type == 3">
						小游戏活动
					</view>
				</view>

				<view class="coupon-list-right-btn">
					<view class="right-btn" @click.stop="handleView(item)" :style="{color:t('color1')}">
						查看活动
					</view>
					<button class="right-btn" open-type="share" @click.stop="handleShare(item)" ref="shareButton"
						:style="{color:t('color1')}">
						分享活动
					</button>

				</view>
			</view>
		</view>
		<!-- 加载更多提示 -->
		<view class="load-more" v-if="loading">
			<text>加载中...</text>
		</view>
		<view class="load-more" v-if="noMore">
			<text>没有更多数据了</text>
		</view>
	</view>
</template>

<script>
	var app = getApp()
	export default {
		data() {
			return {
				datalist: [],
				page: 1,
				limit: 10,
				loading: false,
				noMore: false,
				shareData: {}
			}
		},
		onLoad() {
			this.getlist();
		},

		onReachBottom() {
			if (!this.noMore && !this.loading) {
				this.page++
				this.getlist()
			}
		},
		onShareAppMessage(res) {
			if (res.from === 'button') {
				return this.shareData
			}
		},
		methods: {
			getlist() {
				if (this.loading) return;

				this.loading = true;
				let that = this;

				uni.showLoading({
					title: '加载中'
				});

				app.get('Coupon/getCouponActiveList', {
					page: this.page,
					limit: this.limit,
					type: 0
				}, function(res) {
					uni.hideLoading();
					that.loading = false;

					if (res.code == 200) {
						// 新增：判断是否没有更多数据
						that.noMore = res.data.length < that.limit;

						if (that.page === 1) {
							that.datalist = res.data || [];
						} else {
							that.datalist = [...that.datalist, ...(res.data || [])];
						}
					} else {
						uni.showToast({
							title: res.msg || '加载失败',
							icon: 'none'
						});
					}
				}, function(err) {
					uni.hideLoading();
					that.loading = false;
					uni.showToast({
						title: '网络错误',
						icon: 'none'
					});
				});
			},
			handleView(item) {
				app.goto(`/pagesZone/coupon/hand_coupon?coupon_active_id=${item.coupon_active_id}`)
			},
			handleShare(item) {
				if (item.type == 3) {
					this.shareData = {
						title: item.share_title,
						path: `/pagesZone/temp/cover?coupon_active_id=${item.coupon_active_id}`,
						imageUrl: item.active_pic
					}

				} else {
					this.shareData = {
						title: item.share_title,
						path: `/pagesZone/coupon/get_coupon?coupon_active_id=${item.coupon_active_id}`,
						imageUrl: item.active_pic
					}
				}

				console.log(this.shareData, "share")
				// 模拟点击分享按钮
				this.$nextTick(() => {
					if (this.$refs.shareButton) {
						this.$refs.shareButton.$el.click()
					}
				})
			},

			// 下拉刷新
			onPullDownRefresh() {
				this.page = 1
				this.noMore = false
				this.getlist()
				setTimeout(() => {
					uni.stopPullDownRefresh()
				}, 1000)
			}
		}
	}
</script>

<style scoped>
	.coupon-index {
		padding: 20rpx;
		height: 100vh;
	}

	.coupon-list {
		padding: 20rpx;
		border-radius: 10rpx;
		background-color: rgba(255, 255, 255, 1);
		box-shadow: 0px 4rpx 12rpx 0px rgba(0, 0, 0, 0.4);
		border: 1px solid rgba(187, 187, 187, 1);
		margin-bottom: 40rpx;
		display: flex;
	}

	.coupon-list-left {
		margin-right: 50rpx;
		position: relative;
		width: 200rpx;
		height: 200rpx;
	}

	.coupon-list-left-img {
		width: 100%;
		height: 100%;
		border-radius: 20rpx;
	}

	.money-overlay {
		position: absolute;
		bottom: 20rpx;
		left: 0;
		right: 0;
		text-align: center;
		color: rgba(16, 16, 16, 1);
		font-size: 36rpx;
		font-weight: bold;

	}

	.coupon-list-right {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		flex: 1;
	}

	.coupon-list-right-title {
		color: #fff;
		/* display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden; */

	}

	.coupon-name {
		color: #fff !important;
		font-size: 36rpx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}

	.coupon-list-right-time {
		color: #fff;
		font-size: 26rpx;
	}

	.coupon-list-right-btn {
		display: flex;
		justify-content: flex-end;
		gap: 20rpx;
	}

	.right-btn {
		background: #fff;
		color: rgba(16, 16, 16, 1);
		font-size: 28rpx;
		width: 160rpx;
		height: 54rpx;
		text-align: center;
		line-height: 54rpx;
		border-radius: 56rpx;
		margin: unset;
		font-weight: bold;
	}

	.load-more {
		text-align: center;
		padding: 20rpx;
		font-size: 24rpx;
		color: #999;
	}
</style>